package org.idea.spring.condition.classinject;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * @Author linhao
 * @Date created in 8:05 上午 2021/5/8
 */
@Configuration
public class MyConfig {

    /**
     * @see DependsOn 加载该bean之前需要依赖某个bean，可以达到先后加载bean的顺序目的。
     * @return
     */
    @Bean
//    @ConditionalOnBean(name = "pig2")
    @DependsOn(value = {"pig2"})
//    @ConditionalOnClass
    public Pig pig1() {
        System.out.println("pig1");
        return new Pig(1);
    }

    /**
     * @see org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
     * 如果容器有同"class类型"的bean就不注册,value,type属性均可使用
     *
     * @return
     */
    @Bean(name = "pig2")
    @ConditionalOnMissingBean
//    @ConditionalOnMissingBean
//    @ConditionalOnMissingClass
//    @ConditionalOnMissingBean(value = Pig.class)
    public Pig pig2() {
        System.out.println("pig2");
        return new Pig(2);
    }
}
